package net.oschina.git.fsppp.view.presneter;

import net.oschina.git.fsppp.model.P3Dir;
import net.oschina.git.fsppp.common.baserx.RxSubscribe;
import net.oschina.git.fsppp.model.P3FAT;
import net.oschina.git.fsppp.model.SimpleResponse;
import net.oschina.git.fsppp.view.contract.FileOPContract;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by QiHuang on 2016/10/7.
 */
public class FilePresenter extends FileOPContract.Presenter {
    public FilePresenter(FileOPContract.View view, FileOPContract.Model model) {
        super(view, model);
    }

    @Override
    public void getCurrentFile(String dirPath) {
        mManager.add(mModel.getCurrentFile(dirPath)
                .subscribe(new RxSubscribe<SimpleResponse<List<P3Dir>>>(mFrame) {
                    @Override
                    public void _onNext(SimpleResponse<List<P3Dir>> listSimpleResponse) {
                        mView.returnCurrentFile(listSimpleResponse.getEntity());
                    }
                }));
    }

    @Override
    public void createNewDir(String dirPath, String name) {
        mManager.add(mModel.createNewDir(dirPath, name)
                .subscribe(new RxSubscribe<SimpleResponse<P3Dir>>(mFrame) {
                    @Override
                    public void _onNext(SimpleResponse<P3Dir> p3DirSimpleResponse) {
                        mView.returnNewDirOrFile(p3DirSimpleResponse.getEntity());
                    }
                }));
    }

    @Override
    public void createNewFile(String dirPath, String name) {
        mManager.add(mModel.createNewFile(dirPath, name)
                .subscribe(new RxSubscribe<SimpleResponse<P3Dir>>(mFrame) {
                    @Override
                    public void _onNext(SimpleResponse<P3Dir> p3DirSimpleResponse) {
                        mView.returnNewDirOrFile(p3DirSimpleResponse.getEntity());
                    }
                }));
    }

    @Override
    public void getChildDirs(P3Dir dir) {
        mManager.add(mModel.getChildDirs(dir)
                .subscribe(new RxSubscribe<SimpleResponse<List<P3Dir>>>(mFrame) {
                    @Override
                    public void _onNext(SimpleResponse<List<P3Dir>> listSimpleResponse) {
                        mView.returnCurrentFile(listSimpleResponse.getEntity());
                    }
                }));
    }

    @Override
    public void initDisk() {
        mManager.add(mModel.getInitialDisk()
                .subscribe(new RxSubscribe<SimpleResponse<List<P3Dir>>>(mFrame) {
                    @Override
                    public void _onNext(SimpleResponse<List<P3Dir>> listSimpleResponse) {
                        mView.returnInitialDisk(listSimpleResponse.getEntity());
                    }
                }));
    }

    @Override
    public void geFatList(String diskName) {
        mManager.add(mModel.getFatByDisk(diskName)
                .subscribe(new RxSubscribe<P3FAT>(mFrame) {
                    @Override
                    public void _onNext(P3FAT p3FAT) {
                        mView.returnFatBytes(p3FAT.getContent());
                    }
                }));
    }

    @Override
    public void editFile(P3Dir dir) {
        mManager.add(mModel.editFile(dir)
                .subscribe(new RxSubscribe<SimpleResponse<P3Dir>>(mFrame) {
                    @Override
                    public void _onNext(SimpleResponse<P3Dir> p3DirSimpleResponse) {
                        mView.returnEditedFile(p3DirSimpleResponse.getEntity());
                    }
                }));
    }

    @Override
    public void deleteFile(P3Dir dir) {
        mManager.add(mModel.deleteFile(dir)
                .subscribe(new RxSubscribe<SimpleResponse<P3Dir>>(mFrame) {
                    @Override
                    public void _onNext(SimpleResponse<P3Dir> p3DirSimpleResponse) {
                        mView.returnDelResult(p3DirSimpleResponse.isStatus());
                    }
                }));
    }

    @Override
    public void formatDisk(String diskName) {
        mManager.add(mModel.formatDisk(diskName)
                .subscribe(new RxSubscribe<SimpleResponse>(mFrame) {
                    @Override
                    public void _onNext(SimpleResponse simpleResponse) {
                        mView.returnFormatResult(simpleResponse.isStatus());
                    }
                }));
    }

    @Override
    public void getFileLink(P3Dir dir) {
        mManager.add(mModel.getFileLink(dir)
                .subscribe(new RxSubscribe<LinkedList<Integer>>(mFrame) {
                    @Override
                    public void _onNext(LinkedList<Integer> linkedList) {
                        mView.returnFileLink(linkedList);
                    }
                }));
    }

    @Override
    public void searchFile(String path, String filename) {
        mManager.add(mModel.searchFile(path, filename)
                .subscribe(new RxSubscribe<SimpleResponse<List<P3Dir>>>(mFrame) {
                    @Override
                    public void _onNext(SimpleResponse<List<P3Dir>> listSimpleResponse) {
                        mView.returnSearchResult(listSimpleResponse.getEntity());
                    }
                }));
    }
}
